package com.sky.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.sky.context.BaseContext;
import com.sky.dto.SetmealDTO;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import com.sky.vo.DishVO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
@RequiredArgsConstructor
public class ShopCartServiceImpl implements ShopCartService {
    private final ShopCartMapper shopCartMapper;
    private final DishMapper dishMapper;
    private final SetmealMapper setmealMapper;

    /**
     * 添加商品到购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    @Transactional
    public void addCart(ShoppingCartDTO shoppingCartDTO) {
        //对象拷贝，将ShoppingCartDTO对象转换为ShopCart对象
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        //判断购物车是否存在此商品
        List<ShoppingCart> shoppingCartList = shopCartMapper.getCurrentUserShopCart(shoppingCart);
        if (CollUtil.isNotEmpty(shoppingCartList)) {
            //获取购物车的数据
            ShoppingCart cart = shoppingCartList.get(0);
            //存在则商品数量+1
            shopCartMapper.updateCartNumber(cart);
        } else {
            //不存在则添加购物车，默认数量为1
            if (shoppingCartDTO.getDishId() != null) {
                DishVO dish = dishMapper.getDishById(shoppingCartDTO.getDishId());
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            } else {
                SetmealDTO setmeal = setmealMapper.getById(shoppingCartDTO.getSetmealId());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setUserId(BaseContext.getCurrentId());
            shoppingCart.setCreateTime(LocalDateTime.now());
            //添加购物车
            shopCartMapper.addCart(shoppingCart);
        }

    }

    /**
     * 查询购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> list() {
        ShoppingCart shoppingCart = ShoppingCart.builder().userId(BaseContext.getCurrentId()).build();
        return shopCartMapper.getCurrentUserShopCart(shoppingCart);
    }

    /**
     * 清空购物车
     */
    @Override
    public void clean() {
        shopCartMapper.cleanCart(BaseContext.getCurrentId());
    }

    /**
     * 更新购物车的数量
     *
     * @param shoppingCartDTO
     */
    @Override
    public void subCart(ShoppingCartDTO shoppingCartDTO) {
        Long currentId = BaseContext.getCurrentId();
        //对象拷贝
        ShoppingCart shoppingCart = BeanUtil.copyProperties(shoppingCartDTO, ShoppingCart.class);
        shoppingCart.setUserId(currentId);
        //更新购物车
        shopCartMapper.subCartNumber(shoppingCart);
        List<ShoppingCart> list = shopCartMapper.getCurrentUserShopCartInformation(currentId);
        if (CollUtil.isNotEmpty(list)) {
            List<Long> cartIds = list.stream().map(ShoppingCart::getId).collect(Collectors.toList());
            shopCartMapper.deleteByIds(cartIds);
        }

    }
}
